/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.utils;


public class BindField implements BindFieldInterface {
    
    private String name = null;
    protected Object value = null;
    private String description = null;
    private Bindable bindable = null;
    protected boolean changed = false;
    
    public BindField(String p_name,String p_description,Bindable p_bindable) {
        name = p_name;
        description = p_description;
        bindable = p_bindable;
        if (bindable != null) {
            bindable.addField(this);
        }
    }
    
    public void setBindable(Bindable bindable) {
        this.bindable = bindable;
    }
    
    public void setName(String name) {
        this.name= name;
    }
    
    public String getName() {
        return name;
    }
    
    public String getDescription() {
        return description;
    }
    
    public Bindable getBindable() {
        return bindable;
    }
    
    public String getSerializable() {
        if (value != null) {
            return value.toString();
        } else {
            return null;
        }
    }
    
    public void setSerializable(String p_value) {
        setObject(p_value);
    }
    
    public String getValueAsString() {
        return getSerializable();
    }
    
    public Object getObject() {
        return value;
    }
    
    public void setObject(Object p_value) {
        if ( (value == null && p_value != null) || (value != null && p_value == null) || (p_value != null && !p_value.equals( value ))) {
            changed = true;
            value = p_value;
        }
    }
    
    public void setChanged(boolean changed) {
        this.changed = changed;
    }
    
    public boolean isReadOnly() {
        return false;
    }
    
    public boolean isChanged() {
        return changed;
    }
    
    public boolean getVisibleByDefault() {return true;}
    public void setVisibleByDefault(boolean visible) {;}
    
    public VectorBindFieldRequire getFieldRequirement() {return new VectorBindFieldRequire();}
}

